package com.itheima.secondkillbuyevent;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        //创建商品集合
        ArrayList<Commodity> list = new ArrayList<>();
        //活动结束和开始的时间集合
        ArrayList<LocalDateTime> listTime = new ArrayList<>();
        listTime.add(LocalDateTime.of(2023, 7, 30, 14, 0, 0));
        listTime.add(LocalDateTime.of(2023, 7, 30, 14, 30, 0));


        //添加对象
        list.add(new Commodity(getCommodityNum(list), "高邮咸鸭蛋55g*15枚", 22.8, 2));
        list.add(new Commodity(getCommodityNum(list), "艾美特白天鹅循环扇", 649.0, 9));
        list.add(new Commodity(getCommodityNum(list), "施华洛世奇红瓢虫项链", 599.0, 12));
        list.add(new Commodity(getCommodityNum(list), "HUAWEI P50", 5799.0, 3));

        //
        String str="Y";
        do {
            System.out.println("1.打印所有商品的信息\n" + "2、购买商品\n"+"\n 输入其他任意数字退出");
            int num=scanner.nextInt();
            if (num==1){
                print(list);
            }if (num==2){
                System.out.println("输入活动期限");
                setTime(listTime);
                System.out.println("请输入要购买商品的编号");
                setCommodity(list,scanner.next(),listTime);
            }else {
                break;
            }

        }while (str.equals("Y"));


    }

    //设定活动开始时间和结束时间
    public static ArrayList<LocalDateTime> setTime(ArrayList<LocalDateTime> listTime) {
        Scanner scanner=new Scanner(System.in);
        //创建时间对象
        System.out.println("请输入活动的开始时间");
        LocalDateTime statTime = LocalDateTime.of(scanner.nextInt(), scanner.nextInt(), scanner.nextInt(), scanner.nextInt(), scanner.nextInt(), scanner.nextInt());
        System.out.println("请输入活动结束的时间");
        LocalDateTime endTime = null;
        while (true) {
            endTime = LocalDateTime.of(scanner.nextInt(), scanner.nextInt(), scanner.nextInt(), scanner.nextInt(), scanner.nextInt(), scanner.nextInt());

            if (endTime.isAfter(statTime) == true) {
                System.out.println("结束时间不合理，请重输入");
            } else {
                break;
            }
        }

        listTime.set(0,statTime);
        listTime.set(1,endTime);
        return listTime;
    }

    //判断购买时是否处于活动区间
    public static boolean judgeTime(ArrayList<LocalDateTime> listTime, LocalDateTime localDateTime) {
        if (listTime.get(0).isAfter(localDateTime) == true) {
            if (listTime.get(1).isBefore(localDateTime)) {
                System.out.println("在活动区间购买成功，已享受优惠");
                return true;
            }
        }
        System.out.println("很抱歉，活动已结束");
        return false;
    }

    //打印商品
    public static void print(ArrayList<Commodity> list) {
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).toString());
        }

    }

    //生成商品序号
    public static String getCommodityNum(ArrayList<Commodity> list) {
        while (true) {
            String str = "";
            Random random = new Random();
            //生成六位商品id
            for (int i = 0; i < 6; i++) {
                int num = random.nextInt();
                str += num;
            }
            if (judge(list, str) == -1) {
                return str;
            }
        }

    }

    //判断生成的商品号是否重复
    public static int judge(ArrayList<Commodity> list, String str) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(str) == true) {
                return i;
            }
        }
        return -1;
    }

    //购买
    public static ArrayList<Commodity> setCommodity(ArrayList<Commodity> list, String str, ArrayList<LocalDateTime> listTime) {
        int judge = judge(list, str);

        if (judge != -1) {
            if (list.get(judge).getNum() == 0) {
                System.out.println("对不起，该商品已被抢购完，请留意下次活动的举行");
                return list;
            } else {
                list.get(judge).setNum(-1);
                //获取当前时间
                LocalDateTime nowTime=LocalDateTime.now();
                if ( judgeTime(listTime,nowTime)==true){
                    return list;
                }else {
                    System.out.println("很抱歉，活动已结束");
                }


            }

        }
        return list;
    }

}
